package org.hyperzsoft;

import java.io.IOException;
import java.util.Calendar;

import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;

import android.content.Context;
import android.widget.Toast;

public class utilities {
	private static HttpConnection setupConnection(String url) {
		HttpConnection jsConn = HttpConnection.connect(url);
		
		jsConn.proxy(Constants.PROXY_HOST, Constants.PROXY_PORT, Constants.PROXY_UID, Constants.PROXY_PWD);
		jsConn.userAgent(Constants.USER_AGENT);
		return jsConn;
	}
	
	public static Document getDocument(String url, String... keyvals) {
		try {
			HttpConnection jsConn = setupConnection(url);
			if (keyvals.length > 0)
				jsConn.data(keyvals);
			return jsConn.get();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static String getSeasonValue() {
	    return getSeasonValue(0, true);
	}
	
	public static String getSeasonValue(int years, boolean up) {
		int seasonYrEnd;
		int seasonYrStart;
		Calendar cal = Calendar.getInstance();
		
	    if (cal.get(Calendar.MONTH) >= Calendar.OCTOBER) 
	    	cal.roll(Calendar.YEAR, true);
	    
	    //if (years > 0) cal.roll(Calendar.YEAR, up);
	    
	    seasonYrEnd = cal.get(Calendar.YEAR);
	    
	    seasonYrEnd = (up) ? seasonYrEnd + years : seasonYrEnd - years;
	    
	    seasonYrStart = seasonYrEnd - 1;
	    
	    return Integer.toString(seasonYrStart) + Integer.toString(seasonYrEnd);
	}
	
    public static void showToast(Context context, int id) {
        showToast(context, id, false);
    }
    
    public static void showToast(Context context, int id, boolean longToast) {
        Toast.makeText(context, id, longToast ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show();
    }
    
    public static void showFormattedToast(Context context, int id, Object... args) {
        Toast.makeText(context, String.format(context.getText(id).toString(), args),
                Toast.LENGTH_LONG).show();
    }
}